import { Body, Controller, Get, Post, Put, Param, ParseIntPipe } from '@nestjs/common';
import { PointsRulesService } from './points-rules.service';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags, ApiParam } from '@nestjs/swagger';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { CreatePointsRulesDto, UpdatePointsRulesDto } from './points-rules.dto';
import { AllowAnon } from '~/modules/auth/decorators/allow-anon.decorator';
import { PointsRulesEntity } from './points-rules.entity';

@ApiBearerAuth()
@ApiTags(ModuleSwaggerTags.POINTS)
@Controller('rules')
export class PointsRulesController {
  constructor(private readonly pointsRulesService: PointsRulesService) {}

  @Post()
  @AllowAnon()
  @ApiOperation({ summary: '添加积分规则' })
  @ApiBody({ type: CreatePointsRulesDto })
  async create(@Body() dto: CreatePointsRulesDto): Promise<PointsRulesEntity> {
    return await this.pointsRulesService.create(dto);
  }

  @Get()
  @ApiOperation({ summary: '查询所有积分规则' })
  async findAll(): Promise<PointsRulesEntity[]> {
    return await this.pointsRulesService.findAll();
  }

  @Put(':id')
  @ApiOperation({ summary: '更新积分规则' })
  @ApiParam({ name: 'id', description: '积分规则ID', type: 'number' })
  @ApiBody({ type: UpdatePointsRulesDto })
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdatePointsRulesDto
  ): Promise<PointsRulesEntity> {
    return await this.pointsRulesService.update(id, dto);
  }
}
